<?php
/**
 * Created by PhpStorm.
 * User: Vlad
 * Date: 2/12/14
 * Time: 6:02 PM
 */

class StatisticModel extends BaseModel
{

    private $MODELED_TABLE = "Statistics";
    private static $STATIC_MODELED_TABLE = "Statistics";

    public $StatID;
    public $StatType;
    public $StatValue;
    public $StatInterpretationType;

    public function __construct($statId = 0, $statType = 0, $statValue = 0, $statInterpretationType = 0)
    {
        $this->StatID = $statId;
        $this->StatType = $statType;
        $this->StatValue = $statValue;
        $this->StatInterpretationType = $statInterpretationType;
    }

    // CRUD methods
    public function Create()
    {
        $stmt = DB::Prepare("       insert into $this->MODELED_TABLE (
                                        StatID,     StatType,   StatValue,  StatInterpretationType)
                                    values (
                                        ?,          ?,          ?,          ?)");

        $stmt->bind_param('iidi', $this->StatID, $this->StatType, $this->StatValue, $this->StatInterpretationType);

        $stmt->execute();

        $stmt->close();
    }

    public function ReadByType($statType)
    {
        $stmt = DB::Query("     select
                                            *
                                from
                                            $this->MODELED_TABLE
                                where
                                            StatType = $statType");

        $row = $stmt->fetch_assoc();
        $this->PopulateFromRow($row);
        $stmt->close();
    }

    public static function ReadAll()
    {
        $statisticModelsList = array();

        $modeledTable = self::$STATIC_MODELED_TABLE;

        $stmt = DB::Query("select * from $modeledTable");


        while ($row = $stmt->fetch_assoc())
        {
            $statisticModel = new StatisticModel();
            $statisticModel->PopulateFromRow($row);

            $statisticModelsList[] = $statisticModel;
        }

        $stmt->close();

        return $statisticModelsList;
    }

    public function UpdateByType($statType)
    {
        $stmt = $this->Prepare("        update
                                              $this->MODELED_TABLE
                                        set
                                              StatType = ?
                                        where
                                              StatType = ?");

        $stmt->bind_param('ii', $statType, $statType);

        $stmt->execute();

        $stmt->close();
    }
} 